/*
 * 描述： 全局過濾器配置
 * 修改人： Weng.weng
 * 修改时间： 2020年10月21日
 * 项目： gateway-server
 */
package com.tuozixuan.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import reactor.core.publisher.Mono;

/**
 * 全局過濾器配置<br>
 * 
 * @author Weng.weng
 * @version [版本号, 2020年10月21日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Configuration
public class GlobalFilterConfiguration {
    
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @Bean
    @Order(-1)
    public GlobalFilter globalFilterDemo1() {
        
        return (exchange, chain) -> {
            logger.info("1 pre filter");
            return chain.filter(exchange).then(Mono.fromRunnable(()->{
                logger.info("3 post filter");
            }));
        };
    }
    
    @Bean
    @Order(0)
    public GlobalFilter globalFilterDemo2() {
        
        return (exchange, chain) -> {
            logger.info("2 pre filter");
            return chain.filter(exchange).then(Mono.fromRunnable(()->{
                logger.info("2 post filter");
            }));
        };
    }
    
    @Bean
    @Order(1)
    public GlobalFilter globalFilterDemo3() {
        
        return (exchange, chain) -> {
            logger.info("3 pre filter");
            return chain.filter(exchange).then(Mono.fromRunnable(()->{
                logger.info("1 post filter");
            }));
        };
    }
}
